<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>计算属性，方法与侦听器</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="app">
    {{fullName}}
    {{age}}
  </div>
  <script>
    var vm = new Vue({
      el: "#app",
      data: {
        firstName: "yan",
        lastName: "ming",
        age: 18,
        fullName: "yan ming"
      },  
      // 侦听器
      watch: {
        firstName: function() { // 监听firstName的值，如果发生改变，更新fullName的值
          console.log("计算了一次");
          this.fullName = this.firstName + " " + this.lastName;
        },
        lastName: function () {
          this.fullName = this.firstName + " " + this.lastName;
        }
      }
      // 方法：
      // methods: {
      //   fullName: function () {
      //     console.log("计算了一次");
      //     return this.firstName + " " + this.lastName;
      //   }
      // }
      // 计算属性，内置缓存
      // 控制台vm.age = 17 更改age的值，并不会重新打印 "计算了一次"
      // computed: {
      //   fullName: function() {
      //     console.log("计算了一次");
      //     return this.firstName + " " + this.lastName;
      //   }
      // }
    })
  
  
  
  </script>
</body>
</html>